package ua.com.alus.resvac.crm.stripes.action.account;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.DontValidate;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;
import org.apache.log4j.Logger;
import ua.com.alus.resvac.crm.domen.entities.City;
import ua.com.alus.resvac.crm.domen.entities.VacancyAccountDetails;
import ua.com.alus.resvac.crm.services.spring.BaseService;

import java.util.List;
import java.util.UUID;

/**
 * Register vacancy acc
 * Created by Usatov Alexey
 */
@UrlBinding("/account/RegisterVacancyAccount.action")
public class RegisterVacancyAccountAction extends RegisterAccount {

    private static final Logger LOGGER = Logger.getLogger(RegisterVacancyAccountAction.class);

    @ValidateNestedProperties({
            @Validate(field = "name", required = true, minlength = 3),
            @Validate(field = "fkProfAreaUuid", required = true),
            @Validate(field = "fkCityId", required = true),
            @Validate(field = "icq", mask = "[0-9]+"),
            @Validate(field = "phone", mask = "[\\+][0-9][0-9]+", required = true)
    })
    private VacancyAccountDetails vacancyAccountDetails;

    @SpringBean("/base/baseService")
    private BaseService baseService;

    @DefaultHandler
    @DontValidate
    public Resolution gotoRegisterPage() {
        account = accountService.getAccount(accountService.getUserName());
        return new ForwardResolution("/public/registervacacc.jsp");
    }

    @Override
    public void saveAccount() {
        if (vacancyAccountDetails.getUuid() == null) {
            vacancyAccountDetails.setUuid(UUID.randomUUID().toString());
        }
        vacancyAccountDetails.setAccountUuid(account.getUuid());
        accountService.saveVacancyAccount(account, vacancyAccountDetails);
    }


    public VacancyAccountDetails getVacancyAccountDetails() {
        return vacancyAccountDetails;
    }

    public void setVacancyAccountDetails(VacancyAccountDetails vacancyAccountDetails) {
        this.vacancyAccountDetails = vacancyAccountDetails;
    }

    public City getCurrentCity() {
        if (vacancyAccountDetails.getFkCityId() != null) {
            return baseService.getCityByUuid(vacancyAccountDetails.getFkCityId());
        }
        return null;
    }

    private List<City> cities;

    public List<City> getCities() {
        return cities;
    }

    @DontValidate
    public Resolution updateCities() {
        cities = baseService.getCities(vacancyAccountDetails.getFkRegionId());
        return new ForwardResolution("/public/secured/citiesVacancyAccountFragment.jsp");
    }

    public BaseService getBaseService() {
        return baseService;
    }

}
